/**
 * Copyright 2014 NAVER Corp.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.apesay.plugins;


import com.apesay.core.ProfilerPlugin;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;


/**
 * ClassName:MethodHandlePlugin3
 * Reason:	 
 * Date:     2017/10/27 11:09
 *
 * @author   sid
 */
public class MethodHandlePlugin3 implements ProfilerPlugin {
    static String beanName = "com.apesay.test.User";
    static String methodName = "isAdult";

    public void setup(Boolean open, HashMap<String, HashMap> clsMap) {
        if (open){
            System.out.println(MethodHandlePlugin3.class.getName()+" is used ");


            ClassReader cr = null;
            try {
                cr = new ClassReader(beanName);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
            cr.accept(new UserVisitor(Opcodes.ASM6, cw), Opcodes.ASM6);
            
//
//            //我们接着需要增加一个isAdult方法
//            MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "isChild1","()V", null,null);
//            //开始增加代码
//            mv.visitCode();
//            //接下来，我们需要把新的isAdult方法的内容，增加到这个方法中
//            mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
//            mv.visitLdcInsn("ADD isChild1");
//            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
//            mv.visitInsn(Opcodes.RETURN);
//            mv.visitMaxs(0, 0); //这个地方，最大的操作数栈和最大的本地变量的空间，是自动计算的，是因为构造ClassWriter的时候使用了ClassWriter.COMPUTE_MAXS
//            mv.visitEnd();
            
            
            clsMap.get(TYPE_ASM).put(beanName,cw.toByteArray());
            
            

            try{

                String dirPath = System.getProperty("user.dir");
                System.out.println(" plugin3 rewrite class to :"+dirPath);
                File file = new File(dirPath+"User.class");
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(cw.toByteArray());
                fos.close();

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    public String getKey() {
        return "method.handler3";
    }
}
